package com.fgs.common.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.fgs.common.R;
import com.fgs.common.interf.ISelectItemValue;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

/**
 * @author fgs
 * @date 2019/8/5
 **/
public class SelectAdapter<T> extends RecyclerArrayAdapter<T> {
    private int selectPosition = -1;

    public SelectAdapter(Context context) {
        super(context);
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectViewHolder(parent, R.layout.item_select_dialog);
    }

    private class SelectViewHolder extends BaseViewHolder<T> {
        private ImageView imageView;
        private TextView textView;

        public SelectViewHolder(ViewGroup parent, int res) {
            super(parent, res);
            imageView = getView(R.id.item_select_dialog_imageView);
            textView = getView(R.id.item_select_dialog_textView);
        }

        @Override
        public void setData(T data) {
            super.setData(data);
            textView.setText(getSelectItemText(data));
            if (selectPosition == getDataPosition()) {
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.INVISIBLE);
            }
        }

        private String getSelectItemText(T data) {
            if (data == null) {
                return "";
            } else if (data instanceof ISelectItemValue) {
                ISelectItemValue iSelectItemValue = (ISelectItemValue) data;
                return iSelectItemValue.getSelectItemText();
            } else if (data instanceof String) {
                return data.toString();
            }
            return "";
        }
    }

}
